<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>用户登录</h1>
        <p>请输入您的账号和密码</p>
      </div>
      <form @submit.prevent="login" class="login-form">
        <div class="form-group">
          <label for="username" class="form-label">用户名</label>
          <div class="input-wrapper">
            <i class="icon-user"></i>
            <input
                type="text"
                id="username"
                v-model="user.username"
                required
                class="form-input"
                placeholder="请输入用户名"
            >
          </div>
        </div>
        <div class="form-group">
          <label for="password" class="form-label">密码</label>
          <div class="input-wrapper">
            <i class="icon-lock"></i>
            <input
                type="password"
                id="password"
                v-model="user.password"
                required
                class="form-input"
                placeholder="请输入密码"
            >
          </div>
        </div>
        <div class="remember-forgot">
          <label class="remember-label">
            <input type="checkbox" class="remember-checkbox">
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-link">忘记密码?</a>
        </div>
        <button type="submit" class="login-btn">登录</button>
        <div class="register-link">
          还没有账号? <router-link to="/register">立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import { login } from "@/api/login";
import { setToken, setUserInfo } from "@/utils/token";

export default {
  name: 'UserLogin',
  data() {
    return {
      user: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    login() {
      login(this.user).then(res => {
        if (res.data && res.data.code === 1) {
          // 登录成功逻辑
          const token = res.data.data?.token;
          if (!token) {
            this.$message.error('登录失败：未获取到token');
            return;
          }
          
          // 构建用户信息对象
          const userInfo = {
            username: this.user.username,
            // 如果API返回了更多用户信息，可以添加在这里
            // 例如：nickname: res.data.data?.nickname,
            //       avatar: res.data.data?.avatar
          };
          
          // 使用工具函数存储token和用户信息
          const tokenStored = setToken(token);
          const userInfoStored = setUserInfo(userInfo);
          
          if (tokenStored && userInfoStored) {
            // 添加验证日志
            console.log('token和用户信息存储成功');
            this.$message({ type: 'success', message: '登录成功!' });
            this.$router.push('/home'); // 登录成功后跳转到主页
          } else {
            this.$message.error('登录失败：存储信息时发生错误');
          }
        } else {
          // 登录失败逻辑
          this.$message.error('登录失败: ' + (res.data?.msg || '未知错误'));
        }
      }).catch(err => {
        console.error('登录请求失败:', err);
        this.$message.error('登录失败: ' + (err.response?.data?.msg || '网络错误'));
      })
    }
  }
}
</script>

<style scoped>
/* 全局样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  box-sizing: border-box;
}

/* 登录盒子 */
.login-box {
  width: 100%;
  max-width: 400px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  padding: 30px;
  box-sizing: border-box;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 26px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  font-size: 14px;
  color: #666;
}

/* 表单样式 */
.login-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #555;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
}

.icon-user, .icon-lock {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #888;
  font-size: 16px;
}

.form-input {
  width: 100%;
  padding: 12px 15px 12px 45px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 15px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

/* 记住我和忘记密码 */
.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  font-size: 14px;
}

.remember-label {
  display: flex;
  align-items: center;
  color: #666;
  cursor: pointer;
}

.remember-checkbox {
  margin-right: 6px;
  accent-color: #667eea;
}

.forgot-link {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #556bda;
  text-decoration: underline;
}

/* 登录按钮 */
.login-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.login-btn:hover {
  opacity: 0.9;
  transform: translateY(-2px);
}

.login-btn:active {
  transform: translateY(0);
}

/* 注册链接 */
.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: #556bda;
  text-decoration: underline;
}
</style>